package cn.webrx;

import java.io.File;
import java.io.FileInputStream;

public class Dir6 {
    static long allrows = 0;
    static int count = 0;
    public static void main(String[] args) {
        //String path = "";
        //File f = new File(path);
        //System.out.println(f.getAbsoluteFile());
        //System.out.println(System.getProperty("user.dir"));


        //统计写了多少个java文件
        File f1 = new File("c:/javase2203");
        count(f1);
        //System.out.println(count);
        System.out.printf("有%d个java文件，共有%d行代码。%n",count,allrows);


    }

    public static void count(File dir){
        if(dir.isDirectory()){
            File[] fs = dir.listFiles();
            for(File f : fs){
                if(f.isDirectory()){
                    count(f);
                }else if(f.isFile()  && f.getName().endsWith(".java")){
                    ++count;
                    try(var fis = new FileInputStream(f)){
                        String src = new String(fis.readAllBytes());
                        long rows = src.lines().count();
                        allrows+=rows;
                        System.out.printf("%s(%d行)%n",f.getAbsoluteFile(),rows);
                    }catch(Exception e){

                    }
                }
            }
        }
    }
}
